package com.douqu.game.activity.entity.camp;

import com.bean.core.util.TimeUtils;
import com.douqu.game.activity.GameServer;
import com.douqu.game.activity.entity.BattleServerInfo;
import com.douqu.game.activity.entity.MainServerInfo;
import com.douqu.game.core.e.E_ApplyCampActivityType;
import com.douqu.game.core.e.E_BattleDetailType;
import com.douqu.game.core.e.E_MatchType;
import com.douqu.game.core.entity.activity.MatchBaseInfo;
import com.douqu.game.core.entity.battle.BattleResultParamCampSingle;
import com.douqu.game.core.factory.DataFactory;
import com.douqu.game.core.protobuf.SGCommonProto;
import com.douqu.game.core.util.CoreUtils;
import com.douqu.game.core.util.LogUtils;

import java.rmi.RemoteException;

/**
*
* @Author: Bean
* @Description:
* @Date: 2018-03-09 14:15
*/
public class MatchTeam {

    private String id;

    private MatchTempObject player;

    private MatchTempObject target;

    private E_MatchType matchType;

    private E_BattleDetailType battleDetailType;

    private long createTime;

    private long readyTime;

    public MatchTeam(E_MatchType matchType, E_BattleDetailType battleDetailType, MatchTempObject player, MatchTempObject target)
    {
        this.player = player;
        this.target = target;
        this.matchType = matchType;
        this.battleDetailType = battleDetailType;
        this.createTime = DataFactory.currentTime;

        String index1 = player.getBaseInfo().getObjectIndex();
        String index2 = target.getBaseInfo().getObjectIndex();
        String a = index1.substring(Math.max(0, index1.length() - 4), index1.length());
        String b = index2.substring(Math.max(0, index2.length() - 4), index2.length());
        this.id = a + b + CoreUtils.createUUID(4).toUpperCase();
    }


    public boolean isReady()
    {
        return player.isReady() && target.isReady();
    }

    public void ready(String playerIndex, boolean free)
    {
        if(playerIndex.equals(player.getBaseInfo().getObjectIndex()))
        {
            player.setReady(true);
            player.setFree(free);
        }
        else if(playerIndex.equals(target.getBaseInfo().getObjectIndex()))
        {
            target.setReady(true);
            target.setFree(free);
        }

        if(isReady())
        {
            readyTime = DataFactory.currentTime;

            LogUtils.info(battleDetailType + " 双方都已准备好,即将开战斗 -> " + getVS());
        }
    }

    public boolean isStartBattle()
    {
        return readyTime > 0 && DataFactory.currentTime - readyTime >= 9000;
    }

    public boolean isOverdue()
    {
        return createTime > 0 && DataFactory.currentTime - createTime >= 60000;
    }


    public MatchTempObject getObject(String playerIndex)
    {
        if(playerIndex.equals(player.getBaseInfo().getObjectIndex()))
            return player;

        if(playerIndex.equals(target.getBaseInfo().getObjectIndex()))
            return target;

        return null;
    }


    public MatchTempObject getTarget(String playerIndex)
    {
        if(playerIndex.equals(player.getBaseInfo().getObjectIndex()))
            return target;

        if(playerIndex.equals(target.getBaseInfo().getObjectIndex()))
            return player;

        return null;
    }

    public void startBattle()
    {
        BattleServerInfo freeBattleInfo = GameServer.getInstance().getFreeBattleServer();
        if(freeBattleInfo == null)
        {
            LogUtils.error("战斗服务器没有开启，请检查");
            return;
        }

        String battleId = GameServer.getInstance().createBattleId(player.getBaseInfo().getObjectIndex(), target.getBaseInfo().getObjectIndex());

        int battleType = SGCommonProto.E_BATTLE_TYPE.BATTLE_TYPE_SPAN_CAMP_VALUE;
        boolean battleEnd = false;//是否直接进行结算
        try {
            player.setFree(checkCampBattle(player.getBaseInfo()));
            target.setFree(checkCampBattle(target.getBaseInfo()));
            battleEnd = !player.isFree() && !target.isFree();
            LogUtils.info("队伍1是否空闲(离线表示不空闲) -> " + player.getBaseInfo().getName() + "," + player.isFree() + " 队伍2是否在线 -> " + target.getBaseInfo().getName() + "," + target.isFree());

            //直接结算
            if(battleEnd)
            {
                LogUtils.warn("直接进行结算 －> " + getVS());

                directEndCampBattle(player.getBaseInfo(), target.getBaseInfo());

                if(battleDetailType == E_BattleDetailType.PVP_SPAN_CAMP || battleDetailType == E_BattleDetailType.PVP_DATA_SPAN_CAMP)
                    directEndCampBattle(target.getBaseInfo(), player.getBaseInfo());

                GameServer.getInstance().getActivityCampManager().removeMatchTeam(this);
            }
            else
            {
                MatchTempObject team1 = player;
                MatchTempObject team2 = target;
                if(battleDetailType == E_BattleDetailType.PVP_SPAN_CAMP)
                {
                    if(player.isFree() && !target.isFree())
                    {
                        battleDetailType = E_BattleDetailType.PVP_DATA_SPAN_CAMP;
                    }
                    else if(target.isFree() && !player.isFree())
                    {
                        battleDetailType = E_BattleDetailType.PVP_DATA_SPAN_CAMP;
                        team1 = target;
                        team2 = player;
                    }
                }

                    LogUtils.info("最终战斗类型 -> " + battleDetailType + " -> " + battleId);

                    freeBattleInfo.sendBattleInit(battleId, battleType, battleDetailType.getCode(), team1.getBaseInfo(), team2.getBaseInfo());
                    freeBattleInfo.addStartBattleParam(new StartCampBattleParam(battleId, battleType, battleDetailType.getCode(), team1, team2, freeBattleInfo.getHost(), freeBattleInfo.getPort()));
//                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    private boolean checkCampBattle(MatchBaseInfo player) throws RemoteException
    {
        if(player.getServerId() == 0)
            return false;

        MainServerInfo mainServerInfo = GameServer.getInstance().getMainServerInfo(player.getServerId());
        return mainServerInfo != null && mainServerInfo.getRmiServer().checkCampBattle(player.getObjectIndex());
    }


    private void directEndCampBattle(MatchBaseInfo self, MatchBaseInfo enemy) throws RemoteException
    {
        if(self.getServerId() == 0)
            return;

        MainServerInfo mainServerInfo = GameServer.getInstance().getMainServerInfo(self.getServerId());
        if(mainServerInfo == null)
        {
            LogUtils.error("玩家的主服错误，请检查 -> " + self);
            return;
        }

        BattleResultParamCampSingle result = mainServerInfo.getRmiServer().campBattleEnd(battleDetailType.getCode(),
                self.getFc() > enemy.getFc(), null, enemy, self.getObjectIndex());
        LogUtils.info("直接结算结果 -> " + result);
        GameServer.getInstance().getActivityCampManager().campBattleResult(self.getObjectIndex(), result, null, E_ApplyCampActivityType.NO_BATTLE);
    }





    public MatchTempObject getPlayer() {
        return player;
    }

    public void setPlayer(MatchTempObject player) {
        this.player = player;
    }

    public MatchTempObject getTarget() {
        return target;
    }

    public void setTarget(MatchTempObject target) {
        this.target = target;
    }

    public E_MatchType getMatchType() {
        return matchType;
    }

    public void setMatchType(E_MatchType matchType) {
        this.matchType = matchType;
    }

    public long getCreateTime() {
        return createTime;
    }

    public void setCreateTime(long createTime) {
        this.createTime = createTime;
    }

    public E_BattleDetailType getBattleDetailType() {
        return battleDetailType;
    }

    public void setBattleDetailType(E_BattleDetailType battleDetailType) {
        this.battleDetailType = battleDetailType;
    }

    public String getVS()
    {
        return player.getBaseInfo().getName() + " -VS- " + target.getBaseInfo().getName();
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof MatchTeam)) return false;

        MatchTeam matchTeam = (MatchTeam) o;

        if (!id.equals(matchTeam.id)) return false;

        return true;
    }

    @Override
    public int hashCode() {
        return id == null ? 0 : id.hashCode();
    }


    @Override
    public String toString() {
        return "{" +
                "player=" + player +
                ", target=" + target +
                ", matchType=" + matchType +
                ", battleDetailType=" + battleDetailType +
                ", createTime=" + TimeUtils.getFormatTime(createTime) +
                ", readyTime=" + TimeUtils.getFormatTime(readyTime) +
                '}';
    }
}
